package com.zmn.oms.third.hb114.service;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.utils.HttpUtil;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.third.haobai.HaobaiConsts;
import com.zmn.oms.third.hb114.Haobai114Consts;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 114渠道订单状态变化响应处理实现
 *
 * @author xiewenbing
 * @date   2020/08/12
 */

@Slf4j
@Service("haobai114NotifyService")
public class Haobai114NotifyService implements OrderChangeNotifyService {

    @Autowired
    private OrderWorkService orderWorkService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        if (Objects.isNull(orderWorkLogChange)) {
            return;
        }

        int logType = orderWorkLogChange.getLogType().intValue();

        switch (logType) {
            case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
            case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
            case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                syncAppointTime(orderWorkLogChange);
                break;
            default:
                return;
        }
    }

    /**
     * 同步预约时间
     * @param orderWorkLogChange
     */
    protected void syncAppointTime(OrderWorkLogChange orderWorkLogChange) {

        log.info("【{}】<预约时间同步> ,状态信息:【{}】", Haobai114Consts.CHANNEL_NAME, orderWorkLogChange.toString());

        if (!Haobai114Consts.isHaobai114SyncChannel(orderWorkLogChange.getChannelId())) {
            return;
        }

        if (Objects.isNull(orderWorkLogChange.getDutyTime())) {
            log.info("【{}】预约时间没有修改，不用同步......【{}】", Haobai114Consts.CHANNEL_NAME, orderWorkLogChange.toString());
            return;
        }

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderWorkLogChange.getOrderId(),orderWorkLogChange.getWorkId());
        if (Objects.isNull(orderWork)) {
            log.info("【{}】预约时间同步失败：没有对应的订单信息......【{}】", Haobai114Consts.CHANNEL_NAME, orderWorkLogChange.toString());
            return;
        }

        // 判断城市......安徽、陕西、江西的全部城市
        if (!HaobaiConsts.isNeedSyncOrder(orderWork.getCityId())) {
            log.info("【{}】预约时间同步失败：订单城市无需同步......【{}】", Haobai114Consts.CHANNEL_NAME, orderWork.getCityId());
            return;
        }

        Map<String,Object> mapParamValue = new HashMap<>(5);
        mapParamValue.put("spOrderId",orderWork.getOrderId());
        mapParamValue.put("orderId",orderWork.getOuterId());
        mapParamValue.put("callbackTime",DateUtil.toString(orderWork.getDutyTime()));
        mapParamValue.put("workerName",orderWork.getMasterName());
        mapParamValue.put("workerTel","");

        String syncContent = JSON.toJSONString(mapParamValue);
        String syncResponse;

        try {

            String postUrl = Haobai114Consts.API_URL + Haobai114Consts.SYNC_APPOINTTIME_PATH;
            log.info("【{}】 api请求：url=[{}], request=[{}]", Haobai114Consts.CHANNEL_NAME, postUrl, syncContent);
            syncResponse = HttpUtil.httpPost(postUrl, syncContent);
            log.info("【{}】 api响应：response=[{}]", Haobai114Consts.CHANNEL_NAME, syncResponse);
        } catch (Exception e) {
            log.error("【{}】 请求异常：[{}]", e.getMessage());
        }
    }
}
